package com.jsh.mypor.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsh.mypor.mapper.AdminMapper;
import com.jsh.mypor.pojo.Admin;
import com.jsh.mypor.pojo.LoginForm;
import com.jsh.mypor.pojo.Student;
import com.jsh.mypor.service.AdminService;
import com.jsh.mypor.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Override
    public Admin login(LoginForm loginForm) {
        QueryWrapper<Admin> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("name",loginForm.getUsername())
                        .eq("password", MD5.encrypt(loginForm.getPassword()));
        Admin admin = baseMapper.selectOne(objectQueryWrapper);
        return admin ;
    }

    @Override
    public Admin getAdminById(Long userId) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
      Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;

    }

    @Override
    public IPage<Admin> getAdminByOpr(Page<Admin> pageParam, String adminName) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(adminName)){
            adminQueryWrapper.like("name",adminName);
        }
        adminQueryWrapper.orderByDesc("id");
        Page<Admin> adminPage = baseMapper.selectPage(pageParam, adminQueryWrapper);

        return adminPage;
    }
}
